<template>
  <el-dialog
    :title="toggleTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="400px">
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="rules"
      label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input placeholder="角色名称" v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="所属角色组" prop="parentId">
        <el-select v-model="dataForm.parentId" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="BIID" prop="biId">
        <el-input placeholder="BIID" v-model="dataForm.biId"></el-input>
      </el-form-item>
      <el-form-item label="角色类型" prop="rolesType">
        <el-select v-model="dataForm.rolesType" placeholder="请选择" style="width: 100%;">
          <el-option label="海关" value="cus"></el-option>
          <el-option label="国检" value="ciq"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  // api
  import { getRolesDetail, addOrUpdateRole, getRoleGroupList } from '@/api/roles'

  export default {
    name: 'role-add-or-update',
    data() {
      return {
        visible: false,
        dataForm: {
          id: '',
          routerIds: '',
          name: '',
          parentId: null,
          biId: '',
          rolesType: ''
        },
        rules: {
          name: [
            { required: true, message: '角色名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '角色组不能为空', trigger: 'blur' }
          ]
        },
        options: []
      }
    },
    computed: {
      toggleTitle() {
        return !this.dataForm.id ? '新增' : '编辑'
      },
      addOrUpdate() {
        return !this.dataForm.id ? 'saveRole' : 'updateRole'
      }
    },
    created() {
      this._getRoleGroupList()
    },
    methods: {
      init(id) {
        this.dataForm.id = id || ''
        this.visible = true
        if (this.dataForm.id) {
          getRolesDetail({ id: this.dataForm.id }).then((res) => {
            this.dataForm.name = res.data.data.name
            this.dataForm.parentId = res.data.data.parentId | 0
          })
        } else {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }
      },
      submitForm() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addOrUpdateRole(this.addOrUpdate, this.dataForm).then((res) => {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.visible = false
              this.$emit('refresh')
            })
          } else {
            return false
          }
        })
      },
      close() {
        this.visible = false
        this.$refs['dataForm'].resetFields()
      },
      _getRoleGroupList() {
        getRoleGroupList({ pageNum: 1, pageSize: 999 }).then((res) => {
          res.data.data.dataList.forEach((item) => {
            this.options.push({
              label: item.name,
              value: item.id
            })
          })
        })
      }
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss">

</style>
